package me.zhanshi123.jdbcconnectionbridge;

import com.google.common.base.Charsets;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class ConfigManager {

    private FileConfiguration config = new YamlConfiguration();
    private File f = null;

    public ConfigManager() {
        load();
    }

    public void load() {
        try {
            JdbcConnectionBridge plugin = JdbcConnectionBridge.getInstance();
            f = new File(plugin.getDataFolder(), "config.yml");
            if (!f.exists()) {
                plugin.saveResource("config.yml", false);
            }
            config.load(new BufferedReader(new InputStreamReader(new FileInputStream(f), Charsets.UTF_8)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<String> getMySQL(String identity) {
        if (config.getConfigurationSection("MySQL." + identity) == null) {
            return null;
        }
        return Arrays.asList(config.getString("MySQL." + identity + ".address"),
                config.getString("MySQL." + identity + ".user"),
                config.getString("MySQL." + identity + ".password"),
                config.getString("MySQL." + identity + ".table"));
    }

    public Set<String> getIdentities() {
        ConfigurationSection section = config.getConfigurationSection("MySQL");
        return section.getKeys(false);
    }

}
